package com.gm.baseplatform.base.module;

import java.util.List;

import net.sf.json.JSONArray;

import com.gm.baseplatform.base.BaseConstant;
import com.gm.baseplatform.core.tree.TreeHelper;
import com.gm.baseplatform.core.utils.ObjectUtil;
import com.gm.baseplatform.core.utils.StringUtil;
import com.gm.baseplatform.core.web.BaseActionSupport;

public class ModuleAction extends BaseActionSupport {
	
	private static final long serialVersionUID = 2961380870078192372L;

	private Module module;
	
	public Module getModule() {
		return module;
	}
	public void setModule(Module module) {
		this.module = module;
	}

	public int page;
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	
	public int rows;
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	public String id;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public String list() {
		
	    if (flag.equals(FLAG_LIST)) {
			return SUCCESS;
	    }
	    
	    ModuleManager mm = (ModuleManager)getSpringBean("moduleManager");
	    List<?> firstLevelModule = mm.loadFirstLevelModule();
	    
	    if (flag.equals("tree")) {
	    	if (StringUtil.isNotEmpty(id)) {
	    		getAjaxHelper().outJSN(
		    			TreeHelper.buildTreeJsonText(mm.loadChildModule(id)));
	    	} else {
	    		getAjaxHelper().outJSN(
	    				TreeHelper.buildTreeRootJsonText("模块树",firstLevelModule));
	    	}
	    }
	    
	    if (flag.equals("list_data")) {
	    	 StringBuffer sb = new StringBuffer();
	    	if (StringUtil.isNotEmpty(id)) {
	    		 if (id.equals("0")) {
	    			 id = "-1";
	    		 }
	    		 List<?> childModules = mm.loadChildModule(id);
	    		 sb.append("{\"total\":" + childModules.size());
	    		 sb.append(",\"rows\":");
	    		 sb.append(JSONArray.fromObject(childModules).toString());
	    		 sb.append("}");
	    	} else {
	    		 sb.append("{\"total\":" + firstLevelModule.size());
	    		 sb.append(",\"rows\":");
	    		 sb.append(JSONArray.fromObject(firstLevelModule).toString());
		    	 sb.append("}");
	    	}
    		 getAjaxHelper().outJSN(sb.toString());
	    }
	    
    	return null;
	}
	
	public String add() {
		
		ModuleManager moduleManager = (ModuleManager)getSpringBean("moduleManager");
		
		if (isForwordFlag()) {
			Module parentModule = moduleManager.getModuleByUnid(request.getParameter("parent"));
			if (parentModule == null) {
				parentModule = new Module();
				parentModule.setUnid(Module.NON_PARENT);
				parentModule.setName(Module.NON_PARENT_NAME);
			}
			request.setAttribute("parentModule", parentModule);
			return SUCCESS;
		}
		
		if (isAddFlag()) {
		    if (ObjectUtil.isNotNull(module) && module.isRight()) {
		    	moduleManager.addModule(module);
		    	responseJsonOk(BaseConstant.SAVE_SUCCESS_CN);
		    } else {
		    	responseJsonFailure(BaseConstant.SAVE_FAILURE_CN);
		    }
		}
		
		return null;
	}
}
